//
//  AddressCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/18.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "AddressCon.h"
#import "AddManagerCon.h"
#import "ThirdDetailCell.h"
#import "ShengChenOrderVC.h"
@interface AddressCon ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray *addressArray;
@property(nonatomic,strong)UITableView *listTableview;
@property(nonatomic,strong)UILabel *addresslab;
@property(nonatomic,strong)UIView *footview;
@property(nonatomic,strong)NSString *addressid;
@property(nonatomic,strong)UILabel *sumpricelab;
@property(nonatomic,strong)UILabel *yflab;

@end

@implementation AddressCon

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/address/app/list.do?USER_ID=%@",[UserDefault objectForKey:LogUserID]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [self showDataMethodWithUrl:endstr];
    [self creatFootViewMethod];
    
    
}




-(void)showDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.addressArray = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self creatHeaderViewMethod];
            [self creatTableviewMethod];
        }];
    } failBlock:^(NSError *error) {
        
    }];
}


-(void)creatHeaderViewMethod{
    if (self.addressArray.count > 0) {
        NSDictionary *adddic = self.addressArray[0];
        NSString *address = [NSString stringWithFormat:@"%@ %@\n%@：%@",adddic[@"ADDR_CITY"],adddic[@"ADDR_DETAILS"],adddic[@"ADDR_REALNAME"],adddic[@"ADDR_PHONE"]];
        UILabel *addlab = [UILabel new];
        self.addresslab = addlab;
        self.addressid = adddic[@"ADDRESS_ID"];
        addlab.text = address;
        addlab.numberOfLines = 0;
        addlab.font = [UIFont systemFontOfSize:17*kJLXWidthScale];
        addlab.textColor = [UIColor blackColor];
        [self.view addSubview:addlab];
        addlab.sd_layout
        .topSpaceToView(self.view, 5*kJLXWidthScale)
        .leftSpaceToView(self.view, 5*kJLXWidthScale)
        .rightSpaceToView(self.view, 5*kJLXWidthScale)
        .autoHeightRatio(0);
        addlab.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(AddressManagerAction:)];
        [addlab addGestureRecognizer:tap];
    }else{
        UIView * addBview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50*kJLXWidthScale)];
        addBview.userInteractionEnabled = YES;
        [self.view addSubview:addBview];
        
        UIImageView *imagevi = [UIImageView new];
        imagevi.image = [UIImage imageNamed:@"add_icon.png"];
        [addBview addSubview:imagevi];
        imagevi.sd_layout
        .centerYEqualToView(addBview)
        .leftSpaceToView(addBview, 10*kJLXWidthScale)
        .widthIs(40*kJLXWidthScale)
        .heightIs(40*kJLXWidthScale);
        
        UILabel *lab = [UILabel new];
        lab.text = @"新增地址";
        lab.textColor = [UIColor blackColor];
        lab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        [addBview addSubview:lab];
        lab.sd_layout
        .centerYEqualToView(addBview)
        .leftSpaceToView(imagevi, 8*kJLXWidthScale)
        .rightSpaceToView(addBview, 50*kJLXWidthScale)
        .heightIs(30*kJLXWidthScale);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(AddressManagerAction:)];
        [addBview addGestureRecognizer:tap];
    }

}

-(void)AddressManagerAction:(UITapGestureRecognizer *)tap{
    AddManagerCon *addManVC = [[AddManagerCon alloc]init];
    addManVC.defalAddBlock = ^(NSString *defalAddStr,NSDictionary *adddic) {
        self.addresslab.text = defalAddStr;
        self.addressid = adddic[@"ADDRESS_ID"];
    };
    [self.navigationController pushViewController:addManVC animated:YES];
}

-(void)creatFootViewMethod{
    UIView *footBview = [UIView new];
    self.footview = footBview;
    [self.view addSubview:footBview];
    footBview.sd_layout
    .bottomSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(51*kJLXWidthScale);
    if (kScreenHeight == 812) {
        footBview.sd_layout
        .bottomSpaceToView(self.view, 34)
        .leftSpaceToView(self.view, 0)
        .rightSpaceToView(self.view, 0)
        .heightIs(51*kJLXWidthScale);
    }
    UIView *lineview = [UIView new];
    lineview.backgroundColor = [UIColor orangeColor];
    [footBview addSubview:lineview];
    lineview.sd_layout
    .topSpaceToView(footBview, 0)
    .leftSpaceToView(footBview, 0)
    .rightSpaceToView(footBview, 0)
    .heightIs(1);
    
    UIButton *paybtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [paybtn setTitle:@"确认支付" forState:UIControlStateNormal];
    [paybtn addTarget:self action:@selector(payButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    paybtn.backgroundColor = [UIColor redColor];
    [paybtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footBview addSubview:paybtn];
    paybtn.sd_layout
    .rightSpaceToView(footBview, 1)
    .topSpaceToView(footBview, 0)
    .bottomSpaceToView(footBview, 0)
    .widthIs(80*kJLXWidthScale);
    
    for (int i = 0; i < 2; i ++) {
        UILabel *lab = [UILabel new];
        lab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
        lab.textColor = [UIColor redColor];
        [footBview addSubview:lab];
        lab.sd_layout
        .topSpaceToView(lineview,25*kJLXWidthScale*i)
        .leftSpaceToView(footBview, 10*kJLXWidthScale)
        .rightSpaceToView(paybtn, 0)
        .heightIs(25*kJLXWidthScale);
        if (i == 0) {
            self.sumpricelab = lab;
            lab.text = [NSString stringWithFormat:@"%@",self.sumPriceStr];
        }else{
            self.yflab = lab;
            lab.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
            lab.textColor = LineColor;
            lab.text = [NSString stringWithFormat:@"运费：0.0"];
        }
    }
    
}


-(void)payButtonAction:(UIButton *)sender{
    /*
     接口传参
     GOODS_ID 商品ID（必须）
     ATTRIBUTE_DETAIL_ID 商品属性ID（必须）
     GOODS_COUNT=1 数量（必须）
     USER_ID 用户ID （必须）
     COUPON_ID 优惠券ID（不是必须）
     DDRESS_ID 地址ID (必须)
     否需要分页
     否
     接口地址
     order/app/addorder.do
     返回接口
     map
     多个商品时 GOODS_ID、ATTRIBUTE_DETAIL_ID、GOODS_COUNT  中间用“,” 隔开。
     */
    NSString *strurl = [NSString stringWithFormat:@"/dashop/order/app/addorder.do?GOODS_ID=%@&ATTRIBUTE_DETAIL_ID=%@&GOODS_COUNT=%@&USER_ID=%@&ADDRESS_ID=%@",self.shopId,self.shopsxId,self.shopNumId,[UserDefault objectForKey:LogUserID],self.addressid];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    
    
    ShengChenOrderVC *scvc = [[ShengChenOrderVC alloc]init];
    scvc.payliststr = self.payType;
    __weak typeof(self)weakself = self;
    scvc.successBlock = ^{
//        weakself.seleSCarArray = [NSArray array];
//        weakself.sumpricelab.text = @"0.0";
//        weakself.yflab.text = @"0.0";
//        weakself.sumPriceStr = @"0.0";
        weakself.successBlock();
//        [self.listTableview reloadData];
    };
    scvc.endstr = endstr;
    if (self.seleSCarArray.count > 0) {
        [self.navigationController pushViewController:scvc animated:YES];
    }
    
    
}




-(void)creatTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 60*kJLXWidthScale, kScreenWidth, kScreenHeight - 180*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    if (self.addressArray.count > 0) {
        self.listTableview.sd_layout
        .topSpaceToView(self.addresslab, 5*kJLXWidthScale)
        .bottomSpaceToView(self.footview, 0);
        if (kScreenHeight == 812) {
            self.listTableview.sd_layout
            .topSpaceToView(self.addresslab, 5*kJLXWidthScale)
            .bottomSpaceToView(self.footview, 68);
        }
    }else{
        if (kScreenHeight == 812) {
            self.listTableview.sd_layout
            .bottomSpaceToView(self.view, 34);
        }
    }
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[ThirdDetailCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    
}


#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.seleSCarArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ThirdDetailCell*cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.seleSCarArray[indexPath.row];
    NSArray *shuliangarr = [self.shopNumId componentsSeparatedByString:@","];
    NSString *strurl = [MethodClass returnUrlSetStrWSDLURL:dic[@"GOODS_PIC"]];
    [cell.imagevi sd_setImageWithURL:[NSURL URLWithString:strurl]];
    cell.biaotilab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_NAME"]];
    cell.guigelab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_ATTRIBUTE"]];
    cell.numlab.text = [NSString stringWithFormat:@"%@",shuliangarr[indexPath.row]];
    cell.pricelab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_PRICE"]];
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    return cell;
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
    
    
}



@end
